﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;

namespace Prototype
{
    public class PaymentType
    {
        //private static string connString = ConfigurationManager.ConnectionStrings[0].ToString();

        private static string connString = @"Data Source=MELMAK\SQLEXPRESS;Initial Catalog=Hotel System;Integrated Security=True";
        
        private string name;

        public string Name
        {
            get
            {
                return name;
            }
            set
            {
            }
        }

        internal PaymentType(string name)
        {
            this.name = name;
        }
    
        public static void InsertPaymentType(string name)
        {
            SqlConnection conn = new SqlConnection(connString);
            conn.Open();
            try
            {
                String commandString = "INSERT INTO PaymentType(Name) VALUES (@Name)";
                SqlCommand cmdInsertPayingType = new SqlCommand(commandString, conn);
                SqlParameter paramName = new SqlParameter("@Name", SqlDbType.NVarChar);
                paramName.Value = name;
                cmdInsertPayingType.Parameters.Add(paramName);

                cmdInsertPayingType.ExecuteNonQuery();
            }
            finally
            {
                conn.Close();
            }
        }

        public static PaymentType[] GetAllPaymentTypes()
        {
            List<PaymentType> allPaymentTypes = new List<PaymentType>();
            string[] resultNames = new string[] { };

            SqlConnection conn = new SqlConnection(connString);
            conn.Open();
            try
            {
                String commandString = "SELECT Name FROM PaymentType";
                SqlCommand cmdGetAllPaymentTypes = new SqlCommand(commandString, conn);
                SqlDataReader reader = cmdGetAllPaymentTypes.ExecuteReader();

                using (reader)
                {
                    while (reader.Read())
                    {
                        allPaymentTypes.Add(new PaymentType((string)reader["Name"]));                        
                    }
                }
            }
            finally
            {
                conn.Close();
            }

            foreach (string name in resultNames)
            {
                allPaymentTypes.Add(new PaymentType(name));
            }
            return allPaymentTypes.ToArray();
        }

        public static void RemovePaymentType(string name)
        {
            SqlConnection conn = new SqlConnection(connString);
            conn.Open();
            try
            {
                string commandString = "DELETE FROM PaymentType WHERE Name=@Name";
                SqlCommand cmdRemovePayingType = new SqlCommand(commandString, conn);
                SqlParameter paramName = new SqlParameter("@Name", SqlDbType.NVarChar);
                paramName.Value = name;

                cmdRemovePayingType.ExecuteNonQuery();
            }
            finally
            {
                conn.Close();
            }
        }

        public static void ChangePaymentType(string oldName, string newName)
        {
            SqlConnection conn = new SqlConnection(connString);
            conn.Open();
            try
            {
                string command = "UPDATE PaymentType SET Name=@newName WHERE Name=@oldName";
                SqlCommand cmdChangePayingType = new SqlCommand(command, conn);
                SqlParameter pararName1 = new SqlParameter("@newName", SqlDbType.NVarChar);
                pararName1.Value = newName;
                SqlParameter paramName2 = new SqlParameter("@oldName", SqlDbType.NVarChar);
                paramName2.Value = oldName;

                cmdChangePayingType.ExecuteNonQuery();
            }
            finally
            {
                conn.Close();
            }
        }
    }
}
